<?php
/**
 * 建行接口类
 *
 * lijianjun 161201
 */
defined('ByShopWWI') or exit('Access Invalid!');

class ccb{

	//测试用，直接上代码
    public function __construct(){
		$payOrder='{"TxCode":"MALL10020","TransID":2016024678876,"GoPayType":"2","OrderIniter":"0","BuyerUserID_ThirdSys":"1300188","BuyerUserName_ThirdSys":"橙色云用户","SellerUserID_ThirdSys":"1120","BuyerUserType_ThirdSys":"0","BuyerTrueName_ThirdSys":"00001118","BuyerCompany_ThirdSys":"橙色云用户","BuyerAddress_ThirdSys":"橙子公司","BuyerPhoneNum_ThirdSys":"15900097124","OrderInfos":[{"Order_No":201606463228,"HaveProducts":1,"Order_Products":[{"ProductID":1001,"ProductTitle":"包子1","ProductCode":"C001","ProductModel":"M001","ProductPrice":100,"ProductAmount":0.01,"ProductUnit":"件","ProductDesc":"常规"},{"ProductID":1002,"ProductTitle":"锤子2","ProductCode":"C002","ProductModel":"M002","ProductPrice":32.3,"ProductAmount":0,"ProductUnit":"件","ProductDesc":"常规"}],"Order_Money":0.01,"Order_Time":20160525163227,"Order_Tile":"购货","ReceiverTrueName_ThirdSys":"购货","ReceiverCompany_ThirdSys":"完美用户","ReceiverAddress_ThirdSys":"完美用户","ReceiverCertType_ThirdSys":"17","Order_BuyerPhone":"13800138123", "Expand2":"订单1扩展信息"}]}"';
		$DesKey = "TestDesKey1";
		//echo $payOrder;exit;
		$enStr = $this->des_encrypt($payOrder,$DesKey);
		$Data = $enStr;
		$auth = $this->md5_encrypt("2001"."MALL10020"."2".$Data."TestMD5Key1");
		$html = '<html><head></head><body>';
		$html .= '<form method="post" name="E_FORM" action="http://cmall.ccb.com:8520/alliance/thirdPartAPI.php">';
		$html .= '<input type="hidden" name="ThirdSysID" value="2001"/>';
		$html .= '<input type="hidden" name="TxCode" value="MALL10020"/>';
		$html .= '<input type="hidden" name="RequestType" value="2"/>';
		$html .= '<input type="hidden" name="Data" value="'.$Data.'"/>';
		$html .= '<input type="hidden" name="Auth" value="'.$auth.'"/>';
		$html .= '</form><script type="text/javascript">document.E_FORM.submit();</script>';
		$html .= '</body></html>';
		echo $html;
    }

	//一些方法 demo中的crypt_util.php
	/**
	 **DES 加密
	 **$data 待加密明文
	 **$deskey 加密秘钥
	 **/
	function des_encrypt($data,$deskey){
		$blocksize = mcrypt_get_block_size(MCRYPT_DES,MCRYPT_MODE_ECB);
		$pad = $blocksize - (strlen($data) % $blocksize);
		$data1 = $data. str_repeat(chr($pad),$pad);

		$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB),MCRYPT_RAND); //初始化向量
		@$data_encrypt  = mcrypt_encrypt(MCRYPT_DES,$deskey,$data1,MCRYPT_MODE_ECB);//加密函数
		$datastr = bin2hex($data_encrypt);
		return $datastr;
	}

	/**
	 **DES 解密
	 **$data 待解密密文
	 **$deskey 加密秘钥
	 **/
	function  des_decrypt($endata,$deskey){
		$de_datastr = $endata !== false && preg_match('/^[0-9a-fA-F]+$/i',$endata) ? pack('H*',$endata):false;
		@$data_decrypt = mcrypt_decrypt(MCRYPT_DES,$deskey,$de_datastr,MCRYPT_MODE_ECB,null);//解密函数
		$ret = _pkcs5Unpad($data_decrypt);
		$de_data = trim($ret);
		return $de_data;
	}

	/**
	 **MD5 加密
	 **/
	function md5_encrypt($data){
		return md5($data);
	}

	function _pkcs5Unpad($text){
		$pad = ord($text{strlen($text)-1});
		if($pad > strlen($text)) return false;
		if(strspn($text,chr($pad),strlen($text)-$pad) != $pad) return false;
		$ret = substr($text,0,-1*$pad);
		return $ret;
	}

}